package controller;

import dto.AjaxDTO;
import dto.LoginDTO;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import model.Constants;
import model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import service.IUserService;

@Controller
@RequestMapping({"/login/"})
public class LoginController{
  @Autowired
  private IUserService iUserService;
  
  @RequestMapping({"login"})
  @ResponseBody
  public AjaxDTO login(@RequestBody LoginDTO loginDTO, HttpServletRequest request){
    User user = this.iUserService.getUser(loginDTO);
    AjaxDTO ajaxDto = new AjaxDTO();
    if (user != null) {
      BigDecimal userId = user.getUserId();
      request.getSession().setAttribute(Constants.SESSION_USER_ID, userId.toString());
      ajaxDto.setReturnCode("SUCCESS");
    } else {
      ajaxDto.setReturnCode("FAIL");
    }
    return ajaxDto;
  }
  
  @RequestMapping({"dashboardInit"})
  public String dashboardInit()
  {
    return "index";
  }
  
  @RequestMapping({"loginInit"})
  public String loginInit()
  {
    return "login";
  }
}
